package com.ewili.x;

import com.ewili.framework.utils.ExceptionUtils;
import com.ewili.framework.utils.MathUtils;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;
import com.gargoylesoftware.htmlunit.util.Cookie;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Email163Login {
    public static void main(String[] args) {


        String words = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        List<String> list = MathUtils.permutation(words);

        System.out.println(list.toString());

        //创建一个webclient
        WebClient webClient = new WebClient(BrowserVersion.CHROME);

        //参数设置
        // 1 启动JS
        webClient.getOptions().setJavaScriptEnabled(true);
        // 2 禁用Css，可避免自动二次请求CSS进行渲染
        webClient.getOptions().setCssEnabled(false);
        //3 启动客户端重定向
        webClient.getOptions().setRedirectEnabled(true);
        // 4 运行错误时，是否抛出异常
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        // 5 设置超时
        webClient.getOptions().setTimeout(50000);
        //6 设置忽略证书
        //webClient.getOptions().setUseInsecureSSL(true);
        //7 设置Ajax
        //webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        //8设置cookie
        webClient.getCookieManager().setCookiesEnabled(true);

        //获取页面
        HtmlPage page = null;
        try {
            page = webClient.getPage("http://vip.9sp.cn/admin_account/login.asp");
        } catch (IOException e) {
            ExceptionUtils.propagate(e);
        }

        try {
            Thread.sleep(1000000000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < 1000; i++) {

            // 根据form的名字获取页面表单，也可以通过索引来获取：page.getForms().get(0)
            HtmlForm form = page.getForms().get(0);
            HtmlHiddenInput txtMACAddr = (HtmlHiddenInput) form.getInputByName("txtMACAddr");
            HtmlTextInput username = (HtmlTextInput) form.getInputByName("business_name");
            HtmlPasswordInput password = (HtmlPasswordInput) form.getInputByName("password");
            txtMACAddr.setValueAttribute("");
            username.setValueAttribute("12313");
            password.setValueAttribute("12314");
            HtmlSubmitInput button = (HtmlSubmitInput) page.getHtmlElementById("btnPost");
            try {
                HtmlPage retPage = (HtmlPage) button.click();
            } catch (IOException e) {
                ExceptionUtils.propagate(e);
            }
            // 等待JS驱动dom完成获得还原后的网页
            webClient.waitForBackgroundJavaScript(10000);
            //输出网页内容
            //System.out.println(retPage.asXml());
            //获取cookie
            Set<Cookie> cookies = webClient.getCookieManager().getCookies();
            ;
            Map<String, String> responseCookies = new HashMap<String, String>();
            for (Cookie c : cookies) {
                responseCookies.put(c.getName(), c.getValue());
                System.out.print(c.getName() + ":" + c.getValue());

            }


            //关闭webclient
            webClient.close();
        }

    }
}
